package com.gateway.plugin.global;

import com.gateway.common.constant.Constants;
import com.gateway.common.enums.PluginEnum;
import com.gateway.plugin.api.GatewayPlugin;
import com.gateway.plugin.api.GatewayPluginChain;
import com.gateway.plugin.api.context.GatewayContext;
import com.gateway.plugin.api.context.GatewayContextBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @program: ahcloud-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2024/5/20 09:31
 **/
public class GlobalPlugin implements GatewayPlugin {


    private static final Logger log = LoggerFactory.getLogger(GlobalPlugin.class);
    private final GatewayContextBuilder builder;

    public GlobalPlugin(final GatewayContextBuilder builder) {
        this.builder = builder;
    }

    @Override
    public int getOrder() {
        return PluginEnum.GLOBAL.getCode();
    }

    @Override
    public String named() {
        return PluginEnum.GLOBAL.getName();
    }

    @Override
    public Mono<Void> execute(ServerWebExchange exchange, GatewayPluginChain chain) {
        GatewayContext gatewayContext = builder.build(exchange);
        exchange.getAttributes().put(Constants.CONTEXT, gatewayContext);
        return chain.execute(exchange);
    }
}
